package Work.Optional;

import com.wyt.lzzy.Student;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Stream;

public class OptionalTest {
    public static void main(String[] args) {
        OptionalStream.stream()
                .limit(5)
                .filter(Optional::isPresent)
                .filter(o -> o.get().getAge() > 18)
                .map(Optional::get)
                .forEach(System.out::println);
    }
    public static class OptionalStream extends Student {
        static List<Student> name = Arrays.asList(
                new Student(2, "张华安", 17),
                new Student(11, "陈嘉欣", 21),
                new Student(5, "王龙飞", 18),
                new Student(3, "翁陈列", 19),
                new Student(15, "彭莉娜", 17)

        );
        static Random random = new Random();
        public OptionalStream(int id, String name, int age) {
            super(id, name, age);
        }
        public static Student RandomStu(){
            int num = random.nextInt(3);
            int index = random.nextInt(name.size());
            switch (num){
                case 0: return name.get(index);
                case 1: return name.get(index);
                case 2: return name.get(index);
                default: return null;
            }
        }
        public static Stream<Optional<Student>> stream() {
            return Stream.generate(OptionalStream::RandomStu)
                    .map(student -> Optional.ofNullable(student));
        }
    }

}
